<template>
  <div class="app-container">


    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-circle-plus"
          size="mini"
          @click="handleAdd"
        >添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>


    </el-row>

    <el-table  height="550" style="width: 100%" v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="系统文本配置表id" align="center" prop="textConfigId"  width="180" show-overflow-tooltip/>
      <el-table-column label="唯一标识/一组数据" align="center" prop="configCode" width="180" show-overflow-tooltip />
      <el-table-column label="名称" align="center" prop="configName"  width="180" show-overflow-tooltip/>
      <el-table-column label="值" align="center" prop="configValue"  width="180" show-overflow-tooltip/>
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="配置类型" align="center" prop="configType" >
        <template slot-scope="scope">
          <span v-show="scope.row.configType==1">正常文本</span>
          <span v-show="scope.row.configType==2">富文本</span>
          <span v-show="scope.row.configType==3">图片</span>

        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="mk"  width="180" show-overflow-tooltip/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改系统文本配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="唯一标识" prop="configCode">
          <el-input v-model="form.configCode" placeholder="请输入唯一标识/一组数据" />
        </el-form-item>
        <el-form-item label="名称" prop="configName">
          <el-input v-model="form.configName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="配置类型" prop="configType">
          <el-select @change="jiantingpeizhi(form.configType)" style="width: 100%" v-model="form.configType" placeholder="请选择" clearable size="small">
            <el-option
              v-for="item in optionspzlx"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>

        </el-form-item>

        <el-form-item label="值" >

          <el-input v-show="form.configType==1" v-model="form.configwenben" type="textarea" placeholder="请输入内容" />
          <editor v-show="form.configType==2"  v-model="form.configValue" :min-height="192"/>
          <el-upload
            v-show="form.configType==3"
            class="avatar-uploader"
            :action="uploadImgUrl"
            :show-file-list="false"
            :headers="headers"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="form.configImg" :src="form.configImg"  class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>


        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>

        <el-form-item label="备注" prop="mk">
          <el-input v-model="form.mk" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConfig, getConfig, delConfig, addConfig, updateConfig, exportConfig } from "@/api/lanbangkeji/config/textConfig";
import Editor from '@/components/Editor';
import { getToken } from "@/utils/auth";
export default {
  name: "Config",
  components: {
    Editor,
  },
  data() {
    return {
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
      //uploadImgUrl: "http://172.16.15.14:8082/prod-ap/common/upload", // 上传的图片服务器地址
      // 1正常文本 2富文本 3图片
      optionspzlx:[
        {value:1,label:'正常文本'},
        {value:2,label:'富文本'},
        {value:3,label:'图片'},
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 系统文本配置表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      imageUrl:'',
      zancsunimg:'',
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleAvatarSuccess(res, file) {
      debugger
      // this.imageUrl = URL.createObjectURL(file.raw);
      // console.log(' this.imageUrl', this.imageUrl)
      // this.zancsunimg =  res.url;
      this.form.configImg = res.url;
      debugger
      console.log('上传图片',this.form.configValue)
    },

    jiantingpeizhi(item){
      console.log('监听',item)
      debugger

      if(item==3){
        this.form.configValue = null
      }
    },
    /** 查询系统文本配置列表 */
    getList() {
      this.loading = true;
      listConfig(this.queryParams).then(response => {
        this.configList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        textConfigId: null,
        remark: null,
        createTime: null,
        updateTime: null,
        configCode: null,
        configName: null,
        configValue: '',
        sort: null,
        configType: 1,
        mk: null,
        configImg:'',
        configwenben:''
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.textConfigId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加系统文本配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const textConfigId = row.textConfigId || this.ids
      getConfig(textConfigId).then(response => {
        this.form = response.data;
        if(this.form.configType==1){

          this.form.configwenben = this.form.configValue
        }
        if(this.form.configType==3){

          this.form.configImg = this.form.configValue
        }
        this.open = true;
        this.title = "修改系统文本配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      var that = this
      this.$refs["form"].validate(valid => {
        if (valid) {
          debugger
          if (that.form.textConfigId != null) {
            if(that.form.configType==1){

              that.form.configValue = that.form.configwenben
            }
            if(that.form.configType==3){

              that.form.configValue = that.form.configImg
            }
            updateConfig(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            if(that.form.configType==1){

              that.form.configValue = that.form.configwenben
            }
            if(that.form.configType==3){

              that.form.configValue = that.form.configImg
            }
            addConfig(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const textConfigIds = row.textConfigId || this.ids;
      this.$confirm('是否确认删除系统文本配置编号为"' + textConfigIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delConfig(textConfigIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有系统文本配置数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportConfig(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
